/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.comun;

import java.io.File;
import javax.swing.tree.DefaultMutableTreeNode;
import pfc.arbol.FileTreeNode;
import pfc.generacion.Application;
import pfc.gui.AppFrame;
import pfc.interfaces.ChangeListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.TreeNode;
import org.apache.commons.configuration.CompositeConfiguration;
import pfc.arbol.ArbolFicheros;
import pfc.gui.Editor;
import pfc.interfaces.Salvable;

/**
 *
 * @author xandru
 */
public class IDEpfc implements ChangeListener{

    public static void refreshFileTree(DefaultMutableTreeNode raizficheros) {
        app.refreshFileTree(raizficheros);
    }

    public static void addFileTreeApplication(Application ap, Proyecto proyecto) {
        ArbolFicheros arbol = app.getArbolFicheros();
        arbol.addApplicacion(ap,proyecto);
        arbol.getProyectoFileNode(proyecto);

        FileTreeNode proyectoFileNode = arbol.getProyectoFileNode(proyecto);
        FileTreeNode appNode = new FileTreeNode(ap.getPath());
        proyectoFileNode.add(appNode);
        refreshFileTree(proyectoFileNode);
    }



    ArrayList<Salvable> aGuardar;
    public static AppFrame app;

    AppFrame gui;
    public static CompositeConfiguration conf;

    public IDEpfc(AppFrame gui, CompositeConfiguration conf)
    {
       aGuardar = new ArrayList<Salvable>();
       this.gui = gui;
       this.conf = conf;
    }

    public void addChanged(Salvable s) {
        if (!aGuardar.contains(s))
        {
            aGuardar.add(s);
            gui.enableSaveButton();
        }
    }

    public void saveAll() {
        for (Salvable s: aGuardar )
        {
            s.save();
        }
    }

     public static void main(String[] args)
     {
         try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AppFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AppFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AppFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(AppFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        app = new AppFrame( );
        app.setVisible(true);
     }

    public void removeChanged(Editor aThis) {
        boolean remove = aGuardar.remove(aThis);

        System.out.println(aGuardar.size());
        if (aGuardar.isEmpty())
            gui.disableSaveButton();
    }

    public void initAGuardar() {
        aGuardar = new ArrayList<Salvable>();
    }
    
    public static void refreshTree(TreeNode n) {
        app.refreshTree(n);
    }

    public static CompositeConfiguration getConfiguration()
    {
        return conf;
    }


    public static String getPropertyValue(String propertyKey)
    {
        return conf.getString(propertyKey);
    }


    static public void log(String mensaje)
    {
        app.consola.log(mensaje);
    }

    static public void logTime(String mensaje)
    {
        app.consola.logTime(mensaje);
    }


    public static void updateTextArea(String valueOf) {
        app.consola.log(valueOf);
    }

}
